package com.thunder.reader.transaction;

import android.content.Context;
import android.net.Uri;

/*
 * If one transaction for some group or feed is already on, refuse to launch new one.
 * when canceling, find the transaction from the running queue and cancel it.
 * When app front end is quit, running transactions should be cancelled too.
 */
public abstract class Transaction extends Observable {
    /*
     * Important: by now I understand why there are so many Ints constants in Android,
     * why people do not use enum instead of ints. Because IPC only supports built-in
     * types. So if you want to pass constants to other component(activity, service, 
     * broadcastreceiver), you must pass it along within an Intent which can hold
     * basic built-in types only.
     * If you can make sure you will never pass it to somewhere else, you had better use
     * enum instead of ints.
     */
//    public enum TransactionType {
//        UPDATE_ALL,
//        UPDATE_GROUP,
//        UPDATE_FEED,
//    }
    /**
     * Transaction type, identifying different transaction to process.
     */
    public static final int TRANSACTION_TYPE_UPDATE_FEED = 0;
    public static final int TRANSACTION_TYPE_UPDATE_GROUP = 1;
    public static final int TRANSACTION_TYPE_UPDATE_ALL = 2;
    
    public static final String EXTRA_TRANSACTION_DATA = "transaction_data";
    public static final String EXTRA_TRANSACTION_TYPE = "transaction_type";
    public static final String EXTRA_TRANSACTION_URI = "transaction_uri";
    public static final String EXTRA_GROUP_ID = "group_id";
    public static final String EXTRA_SOURCE_ID = "source_id";
    
    protected TransactionData mData;
    protected Context mContext;
    
    public Transaction(Context context, TransactionData data) {
        mData = data;
        mContext = context;
    }
    
    public abstract void process();
    
    public abstract void cancelProcess();

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((mData == null) ? 0 : mData.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Transaction other = (Transaction) obj;
        if (mData == null) {
            if (other.mData != null) {
                return false;
            }
        } else if (!mData.equals(other.mData)) {
            return false;
        }
        return true;
    }
}